<?php

class ManageMeals extends Controller {

	function ManageMeals()
	{
		parent::Controller();	
	}
	
	function index()
	{
		// get the meals for the day, if any
		$data['query'] = $this->db->query("
				SELECT 
				meal_name,
				meal_link,
				id,
				meal_id,
				date_submitted,
				deleted
				FROM
				meals,
				meals_to_days
				WHERE
				id = meal_id
				AND
				date_submitted = '". date('Y-m-d') ."'
				AND
				deleted = '0'	
		");
		if($data['query']->num_rows() > 0)
			$data['haveMeals'] = true;
		else
			$data['haveMeals'] = false;
		$data['title']="Today's Meal Options";
		$this->load->view('header', $data);
		$this->load->view('todays_meals', $data);
		$this->load->view('footer');
	}
	
	function meals_insert()
	{
		$the_meal = array(
			'meal_name' => $_POST['meal_name'],
			'meal_link' => $_POST['meal_link']
			);
		$this->db->insert('meals', $the_meal);
		
		$the_meal_to_date = array(
			'meal_id' => mysql_insert_id(),
			'date_submitted' => $_POST['date_submitted']
			);
		$this->db->insert('meals_to_days', $the_meal_to_date);
		redirect('managemeals/index/');	
	
	}
	
	function meal_veto()
	{
	/*	$this->db->delete('meals_to_days', array(
									'meal_id' => $this->uri->segment(3),
									'date_submitted' => date('Y-m-d')
		));*/
		$data = array('deleted' => 1);
		$this->db->where('meal_id',$this->uri->segment(3));
		$this->db->where('date_submitted',date('Y-m-d'));
		$this->db->update('meals_to_days',$data);
		$this->session->set_flashdata('deleted',$this->uri->segment(3));
		redirect('managemeals/index/');
			
		
	}
}
